package org.shoukaiseki.common.exception;

import org.apache.xmlbeans.impl.xb.xsdschema.AnyDocument;
import org.shoukaiseki.common.utils.StringUtils;

//import javax.validation.constraints.NotNull

/**
 * 用于提示前端消息的虚拟异常
 * org.shoukaiseki.common.exception.MessageVirtualException
 * <br></br>
 *
 * @author shoukaiseki ブログ http://www.shoukaiseki.top/<br></br> E-メール jiang28555@Gmail.com<br></br>
 * @date 2020-12-21 11:35:40<br></br>
 */
public class MessageVirtualException extends RuntimeException {

    private String message;

    private String userMessage;

    private boolean htmlMsg=false;



    public MessageVirtualException(String message)  {
        super(message);
        this.message=super.getMessage();
    }

    public MessageVirtualException(String message,Throwable t)  {
        super(message,t);
        this.message=super.getMessage();
    }

    public MessageVirtualException setMessage(String message) {
        this.message = message;
        return this;
    }

    //    override var message: String=""
//        get() {
//            if (super.cause != null && super.cause is MessageVirtualException) {
//                return field + cause?.message
//
//            } else {
//                return field
//            }
//        }
//        set(value) {field=value}


    public MessageVirtualException format(Object... params){
        if (this.message==null) {
            return this;
        }
        //可变参数向下传递时加上 *
        this.message=StringUtils.format(this.message,params);
        return this;
    }

    @Override
    public String getMessage() {
       if (super.getCause() != null && super.getCause() instanceof  MessageVirtualException) {
           return  message + super.getCause().getMessage();
        } else {
           return message;
       }
    }

    public boolean isHtmlMsg() {
        return htmlMsg;
    }

    public MessageVirtualException setHtmlMsg(boolean htmlMsg) {
        this.htmlMsg = htmlMsg;
        return this;
    }

    public String getUserMessage() {
        return userMessage;
    }

    public MessageVirtualException setUserMessage(String debugMessage,Object... params) {
        if(StringUtils.isEmpty(debugMessage)){
            this.userMessage=debugMessage;
            return this;
        }
        this.userMessage=StringUtils.format(debugMessage,params);
        return this;
    }
}
